<?php
namespace app\admin\controller;
use think\Db;
class Shopercode extends Base{
    public function index(){
        if(request()->isAjax ()){
            extract (input ());
            $map = [];
            if(isset($status)&&$status != ""){
                if($status == 1){
                    $map['ss_shop_id'] = ['<=',0];
                }else{
                    $map['ss_shop_id'] = ['>',0];
                }

            }
            if(isset($key)&&$key!="")
            {
                $map['shop_account|shop_name|ss_uni_name'] = ['like',"%" . $key . "%"];
            }
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('shop_ercode ss')
                ->join('shop s','ss.ss_shop_id = s.shop_id','left')
                ->where($map)
                ->count();
            $lists = Db::name('shop_ercode ss')
                ->join('shop s','ss.ss_shop_id = s.shop_id','left')
                ->where($map)
                ->page($Nowpage,$limits)
                ->field('ss.*,shop_account,shop_name')
                ->select ();
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        return $this->fetch();
    }
    /**
     * 导入Excel
     * @return mixed
     */
    public function excelin(){
        set_time_limit(0);
        if(request()->file('file')){
            $file = request()->file('file');
            $info  = $file->getInfo();
            $file_path = $info['tmp_name'];
            //取出文件后缀
            $file_type = explode(".",$info['name']);
            $file_type = end($file_type);
            vendor("PHPExcel.PHPExcel.IOFactory");
            if( $file_type == 'xls' ){
                $inputFileType = 'Excel5';    //这个是读 xls的
            }else{
                $inputFileType = 'Excel2007';//这个是计xlsx的
            }
            $objReader = \PHPExcel_IOFactory::createReader($inputFileType);
            $objPHPExcel = $objReader->load($file_path);
            $objWorksheet = $objPHPExcel->getActiveSheet();
            $highestRow = $objWorksheet->getHighestRow();
//            print_r($highestRow);die;
            $time = date('Y-m-d H:i:s');
            $data = [];
            for ($i=1;$i<=$highestRow;$i++) {
                $name = $objPHPExcel->getActiveSheet ()->getCell ("A" . $i)->getValue ();//
                if(trim($name,' ') != ''){
                    $name = trim($name,' ');
                    $have = Db::table('up_shop_ercode')
                        ->where('ss_uni_name', $name)
                        ->find();
                    if($have){
                        return json(['code' => 100, 'msg' => "{$name}已存在！"]);
                    }else{
                        $ss_urlparam = md5(time() . $i);
                        $insert = [
                            'ss_uni_name' => $name,
                            'ss_urlparam' => $ss_urlparam,
                            'ss_url' => 'https://csdl.jintianjifen.cn/admin/Share/equipment?urlsecretkey=' . $ss_urlparam,
                            'ss_addtime' => $time
                        ];
                        $data[] = $insert;
                    }
                }
            }
            if($data){
                $res = Db::table('up_shop_ercode')->insertAll($data);
                if($res){
                    return json(['code' => 200,'msg'=>"导入成功"]);
                }else{
                    return json(['code' => 100,'msg'=>"导入失败"]);
                }
            }else{
                return json(['code' => 100,'msg'=>"有效数据为空"]);
            }
        }
        return $this->fetch();
    }
    public function setCode(){
        extract (input());
        if(request()->isPost ()){
            $shop = Db::table('up_shop')
                ->where('shop_account', $phone)
                ->field('shop_id,shop_attestation,shop_status')
                ->find();
            if (!$shop) return json(['code' => 100, 'msg' => '您还不是商家']);
            if ($shop['shop_attestation'] != 2) return json(['code' => 100, 'msg' => '只有认证通过的商家才可以绑定']);
            if ($shop['shop_status'] != 1) return json(['code' => 100, 'msg' => '此账号已被冻结']);
            $have = Db::table('up_shop_ercode')
                ->where('ss_id', '<>',$ss_id)
                ->where('ss_shop_id', $shop['shop_id'])
                ->find();
            if ($have) return json(['code' => 100, 'msg' => '此商家已经绑定过设备了']);
            $ss_about_name = strtoupper($ss_about_name);
            $have = Db::table('up_shop_ercode')
                ->where('ss_id', '<>',$ss_id)
                ->where('ss_about_name', $ss_about_name)
                ->find();
            if ($have) return json(['code' => 100, 'msg' => '此设备已经绑定商家了']);
            $have = Db::table('up_shop_ercode')
                ->where('ss_uni_name', $ss_about_name)
                ->find();
            if (!$have) return json(['code' => 100, 'msg' => '无此设备，请检查好填写']);
            $res = Db::table('up_shop_ercode')
                ->where('ss_id', $ss_id)
                ->update([
                    'ss_shop_id' => $shop['shop_id'],
                    'ss_about_name' => $ss_about_name,
                    'ss_bindtime' => date('Y-m-d H:i:s')
                ]);
            if ($res) {
                return json(['code' => 200, 'msg' => '绑定成功']);
            } else {
                return json(['code' => 100, 'msg' => '绑定失败']);
            }
        }
        $data = $lists = Db::name('shop_ercode ss')
            ->join('shop s','ss.ss_shop_id = s.shop_id','left')
            ->where('ss_id',$ss_id)
            ->field('ss.*,shop_account,shop_name')
            ->find ();
        return $this->fetch ('shopercode/setcode',['data'=>$data]);
    }
}